<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_plant_grass\Entity\ShusyPlantGrass;
use EntityListWrapper;

/**
 * @OA\Schema(
 *   schema="PlantGrassItem",
 *   type="object",
 *   description="PlantGrassItem"
 * )
 */
class PlantGrassItem extends AbstractBaseModel {

  protected function getEntityType() {
    return 'shusy_plant_grass';
  }

  protected function setValue() {
    if ($this->entity instanceof ShusyPlantGrass) {
      $value = [
        /**
         * @OA\Property(
         *   property="id",
         *   type="integer",
         *   description="ID",
         * ),
         */
        'id' => (int) $this->entity->id,
        /**
         * @OA\Property(
         *   property="title",
         *   type="string",
         *   description="标题",
         * ),
         */
        'title' => (string) $this->entity->title,
        /**
         * @OA\Property(
         *   property="type",
         *   type="integer",
         *   description="类型 0 图文 1 视频",
         * ),
         */
        'type' => (int) $this->entity->type,
        /**
         * @OA\Property(
         *   property="image",
         *   type="string",
         *   description="封面图",
         * ),
         */
        'image' => $this->getImage(),
        /**
         * @OA\Property(
         *   property="video",
         *   type="string",
         *   description="视频",
         * ),
         */
        'video' => $this->getVideo(),
        /**
         * @OA\Property(
         *   property="user",
         *   ref="#/components/schemas/User",
         *   description="作者",
         * ),
         */
        'user' => User::get($this->entity->uid),
        /**
         * @OA\Property(
         *   property="like",
         *   type="string",
         *   description="点赞数 默认是数字 如果数字太大 可能会转换单位 例:1.5k 2w等",
         * ),
         */
        'like' => $this->getLike($this->entity),
        /**
         * @OA\Property(
         *   property="isLike",
         *   type="integer",
         *   description="是否点赞 0 否 1 是",
         * ),
         */
        'isLike' => (int) $this->getIsLike($this->entity),
        /**
         * @OA\Property(
         *   property="created",
         *   type="string",
         *   description="发布时间",
         * ),
         */
        'created' => $this->formatDate($this->entity->created, 'Y/m/d'),
      ];
      $this->merge($value);
    }
  }

  protected function getImage() {
    $field_images = $this->wrapper->field_images;
    if ($field_images instanceof EntityListWrapper) {
      foreach ($field_images as $image) {
        $field_image = $image->value();
        return $this->imageStyleUrl('PlantGrassItemImage', $field_image);
        break;
      }
    }
    return '';
  }

  protected function getVideo() {
    $field_videos = $this->wrapper->field_videos;
    if ($field_videos instanceof EntityListWrapper) {
      foreach ($field_videos as $field_video) {
        $video = $field_video->value();
        return file_create_url($video['uri']);
        break;
      }
    }
    return '';
  }

  protected function getLike(ShusyPlantGrass $grass) {
    $query = db_select('shusy_plant_grass_like', 'l');
    $query->fields('l', ['id']);
    $query->condition('plant_grass_id', $grass->id);
    $query->condition('status', 1);
    $result = $query->execute()->rowCount();
    return $result;
  }

  protected function getIsLike(ShusyPlantGrass $grass) {
    $query = db_select('shusy_plant_grass_like', 'l');
    $query->fields('l', ['id']);
    $query->condition('plant_grass_id', $grass->id);
    $query->condition('uid', $this->getUid());
    $query->condition('status', 1);
    $result = $query->execute()->rowCount();
    return $result;
  }
}
